const app = getApp()
import { baseUrl } from "../../../../../api/request";

Page({
  data: {
    formats: {},
    placeholder: '开始输入...',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false,
    navHeight: 32,
    navTop: 48,
    key: null,
  },
  onLoad(e) {
    this.data.key = e.key;
    const platform = wx.getSystemInfoSync().platform;
    const isIOS = platform === 'ios'
    this.setData({ isIOS })
    const that = this
    this.updatePosition(0)
    let keyboardHeight = 0
    wx.onKeyboardHeightChange(res => {
      if (res.height === keyboardHeight) return
      const duration = res.height > 0 ? res.duration * 1000 : 0
      keyboardHeight = res.height
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: 0,
          success() {
            that.updatePosition(keyboardHeight)
            that.editorCtx.scrollIntoView()
          }
        })
      }, duration)
    })
  },
  updatePosition(keyboardHeight) {
    const toolbarHeight = 50
    const { windowHeight, platform } = wx.getSystemInfoSync()
    let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
    this.setData({ editorHeight, keyboardHeight })
  },
  calNavigationBarAndStatusBar() {
    const systemInfo = wx.getSystemInfoSync()
    const { statusBarHeight, platform } = systemInfo
    const isIOS = platform === 'ios'
    const navigationBarHeight = isIOS ? 44 : 48
    return statusBarHeight + navigationBarHeight
  },
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
      that.editorCtx.setContents({
        html: app.globalData.introduceStr || '',
      })
    }).exec()
  },
  intChg(e) {
    if (e.type == "blur") {
      this.editorCtx.blur()
    }
  },
  format(e) {
    let { name, value } = e.target.dataset
    if (!name) return
    // console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
  onStatusChange(e) {
    const formats = e.detail
    this.setData({ formats })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function () {
        console.log('insert divider success')
      }
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function (res) {
        console.log("clear success")
      }
    })
  },
  removeFormat() {
    this.editorCtx.removeFormat()
  },
  saveback() {
    let pages = getCurrentPages();
    let prePage = pages[pages.length - 2];
    let that = this;
    this.editorCtx.getContents({
      success: (res) => {
        prePage.setData({
          ['form.' + that.data.key]: res.html
        })
        app.globalData.introduceStr = '';
        wx.navigateBack()
      }
    })

  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate
    })
  },
  insertImage() {
    const that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        wx.uploadFile({
          url: baseUrl + '/common/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data",
          },
          formData: null,
          success: function (response) {
            let data = JSON.parse(response.data)
            if (data.code == 200) {
              that.editorCtx.insertImage({
                src: baseUrl + data.fileName[0],
                data: {
                  id: 'abcd',
                  role: 'god'
                },
                width: '80%',
              })
            } else {
              wx.showToast({
                icon: 'none',
                title: '图片上传失败',
              })
            }
          },
          fail: function (err) {
            wx.showToast({
              icon: 'none',
              title: '图片上传失败',
            })
          },
        })

      }
    })
  }
})
